package com.example.demo.leetcode.list;

/**
 * 简单的单向链表
 *
 * @author luowj12
 * @since 2021/6/2 14:07
 */
public class SingleLinkedList {

    private int size; //链表节点的个数
    private Node head;

    public SingleLinkedList(){
        size = 0;
        head = null;
    }

    private class Node{
        private   Object data; //每个节点的数据
        private Node next; //每个节点指向下一个节点的连接

        public Node(Object data){
            this.data = data;
        }
    }

    //在链表头部添加元素
    public Object addHead(Object obj){
        Node newhead = new Node(obj);
        if(size == 0){
            head = newhead;
        }else{
            newhead.next = head;
            head = newhead;
        }
        size++;
        return obj;
    }

    //在单链表头部进行删除
    public Object delHead(){
        Object obj = head.data;
        head = head.next;
        size--;
        return obj;
    }
}
